package com.sdhs.paas.itims.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;
import com.sdhs.paas.itims.bean.AppInfoBean;
import com.sdhs.paas.itims.bean.ItmsReportHistoryBean;
import com.sdhs.paas.itims.dto.AppInfoDTO;
import com.sdhs.paas.itims.dto.ItmsReportHistoryDTO;
import com.sdhs.paas.itims.dto.req.ItmsReportHistoryReqDTO;
import com.sdhs.paas.itims.dto.res.AppInfoResDTO;
import com.sdhs.paas.itims.dto.res.ItmsReportHistoryResDTO;
import com.sdhs.paas.itims.service.ItmsReportHistoryService;

/**
 * @author ItmsReportHistory
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-10-31
 */
 
@RefreshableRestController
@RequestMapping(value = "/itmsReportHistory")
public class ItmsReportHistoryController {

    @Autowired
    private ItmsReportHistoryService itmsReportHistoryService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getItmsReportHistoryPageList")
	public PaasBaseResponse<ItmsReportHistoryResDTO> getItmsReportHistoryPageList(@RequestBody PaasBaseRequest<ItmsReportHistoryReqDTO> itmsReportHistoryReqDTO) {
    	ItmsReportHistoryDTO itmsReportHistoryDTO = itmsReportHistoryReqDTO.getBody().getItmsReportHistoryDTO();
    	PageInfoDTO pageInfoDTO = itmsReportHistoryReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
    	
		ItmsReportHistoryBean itmsReportHistoryBean = EntityDtoConvertUtil.convertToEntity(itmsReportHistoryDTO, ItmsReportHistoryBean.class);
    	Page<ItmsReportHistoryBean> page = itmsReportHistoryService.getPageItmsReportHistoryList(pageRequest, itmsReportHistoryBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	ItmsReportHistoryResDTO itmsReportHistoryResDTO = new ItmsReportHistoryResDTO();
    	resPageInfoDTO.setPage(page.getCurPage());
		resPageInfoDTO.setPageSize(page.getPageSize());
		resPageInfoDTO.setPageCount(page.getPageCount());
		resPageInfoDTO.setRowCount(page.getTotalCount());
		itmsReportHistoryResDTO.setPageInfo(resPageInfoDTO);
		List<ItmsReportHistoryBean> reportHistoryBeanList = page.getResult();
		List<ItmsReportHistoryDTO> dtoList = new ArrayList<ItmsReportHistoryDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(reportHistoryBeanList, new TypeReference<List<ItmsReportHistoryDTO>>() {
		});
		itmsReportHistoryResDTO.setItmsReportHistoryDTOList(dtoList);
    	return new PaasBaseResponse<>(itmsReportHistoryResDTO);
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveItmsReportHistory")
	public PaasBaseResponse<ItmsReportHistoryResDTO> saveItmsReportHistory(@RequestBody PaasBaseRequest<ItmsReportHistoryReqDTO> itmsReportHistoryReqDTO) {
		ItmsReportHistoryDTO itmsReportHistoryDTO = itmsReportHistoryReqDTO.getBody().getItmsReportHistoryDTO();
		ItmsReportHistoryBean itmsReportHistoryBean = EntityDtoConvertUtil.convertToEntity(itmsReportHistoryDTO, ItmsReportHistoryBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		itmsReportHistoryBean.setId(uuid);
		itmsReportHistoryBean.setIsDeleted(0);
		Boolean flag = itmsReportHistoryService.saveItmsReportHistory(itmsReportHistoryBean);
		itmsReportHistoryDTO.setId(uuid);
		ItmsReportHistoryResDTO itmsReportHistoryResDTO = new ItmsReportHistoryResDTO();
		itmsReportHistoryResDTO.setItmsReportHistoryDTO(itmsReportHistoryDTO);
		return new PaasBaseResponse<>(itmsReportHistoryResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateItmsReportHistory")
	public PaasBaseResponse<ItmsReportHistoryResDTO> updateItmsReportHistory(@RequestBody PaasBaseRequest<ItmsReportHistoryReqDTO> itmsReportHistoryReqDTO) {
		ItmsReportHistoryDTO itmsReportHistoryDTO = itmsReportHistoryReqDTO.getBody().getItmsReportHistoryDTO();
		ItmsReportHistoryBean itmsReportHistoryBean = EntityDtoConvertUtil.convertToEntity(itmsReportHistoryDTO, ItmsReportHistoryBean.class);
		Boolean flag = itmsReportHistoryService.updateItmsReportHistory(itmsReportHistoryBean);
		ItmsReportHistoryResDTO itmsReportHistoryResDTO = new ItmsReportHistoryResDTO();
		itmsReportHistoryResDTO.setItmsReportHistoryDTO(itmsReportHistoryDTO);
		return new PaasBaseResponse<>(itmsReportHistoryResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getItmsReportHistoryDetail")
	public PaasBaseResponse<ItmsReportHistoryResDTO> getItmsReportHistoryDetail(@RequestBody PaasBaseRequest<ItmsReportHistoryReqDTO> itmsReportHistoryReqDTO) {
		ItmsReportHistoryDTO itmsReportHistoryDTO = itmsReportHistoryReqDTO.getBody().getItmsReportHistoryDTO();
		ItmsReportHistoryBean itmsReportHistoryBean = EntityDtoConvertUtil.convertToEntity(itmsReportHistoryDTO, ItmsReportHistoryBean.class);
		itmsReportHistoryBean = itmsReportHistoryService.getItmsReportHistoryDetail(itmsReportHistoryBean.getId());
		itmsReportHistoryDTO = EntityDtoConvertUtil.convertToDTO(itmsReportHistoryBean, ItmsReportHistoryDTO.class);
		ItmsReportHistoryResDTO itmsReportHistoryResDTO = new ItmsReportHistoryResDTO();
		itmsReportHistoryResDTO.setItmsReportHistoryDTO(itmsReportHistoryDTO);
		return new PaasBaseResponse<>(itmsReportHistoryResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteItmsReportHistory")
	public PaasBaseResponse<ItmsReportHistoryResDTO> deleteItmsReportHistory(@RequestBody PaasBaseRequest<ItmsReportHistoryReqDTO> itmsReportHistoryReqDTO) {
		ItmsReportHistoryDTO itmsReportHistoryDTO = itmsReportHistoryReqDTO.getBody().getItmsReportHistoryDTO();
		ItmsReportHistoryBean itmsReportHistoryBean = EntityDtoConvertUtil.convertToEntity(itmsReportHistoryDTO, ItmsReportHistoryBean.class);
		Boolean flag = itmsReportHistoryService.deleteItmsReportHistory(itmsReportHistoryBean);
		ItmsReportHistoryResDTO itmsReportHistoryResDTO = new ItmsReportHistoryResDTO();
		return new PaasBaseResponse<>(itmsReportHistoryResDTO);
	}
}
